﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Commands;
using WpfPostgresql.Model;

namespace WpfPostgresql.ViewModel
{
    class PosilkaViewModel : SkladViewModel
    {
        public ObservableCollection<Databases.Magaz> Magazins { get; set; }
        public ObservableCollection<Databases.Voditel> Voditels { get; set; }
        public ObservableCollection<Databases.Tovar> Tovars { get; set; }

        private ObservableCollection<Databases.Tovar> _tovarsTemp;
        public ObservableCollection<Databases.Tovar> TovarsTemp
        {
            get { return _tovarsTemp; }
            set { _tovarsTemp = value; RaisePropertyChanged("TovarsTemp"); }
        }

        private Databases.Tovar _selectedTovar;
        public Databases.Tovar SelectedTovar
        {
            get { return _selectedTovar; }
            set { _selectedTovar = value; RaisePropertyChanged("SelectedTovar"); }
        }

        private Databases.Magaz _selectedMagaz;
        public Databases.Magaz SelectedMagaz
        {
            get { return _selectedMagaz; }
            set { _selectedMagaz = value; RaisePropertyChanged("SelectedMagaz"); }
        }

        private Databases.Voditel _selectedVoditel;
        public Databases.Voditel SelectedVoditel
        {
            get { return _selectedVoditel; }
            set { _selectedVoditel = value; RaisePropertyChanged("SelectedVoditel"); }
        }


        public PosilkaViewModel()
        {
            _sendCommand = new DelegateCommand(Send);
            _addCommand = new DelegateCommand(AddNewTovar);
            _delCommand = new DelegateCommand<Databases.Tovar>(DelTovar);
            _tovarsTemp = new ObservableCollection<Databases.Tovar>();
            Tovars = new ObservableCollection<Databases.Tovar>(Databases.GetTovarListWithoutAddedInMagaz(this));
            
            _selectedTovar = Tovars.First();
            Magazins = new ObservableCollection<Databases.Magaz>(Databases.GetMagazList());
            _selectedMagaz = Magazins.First();
            Voditels = new ObservableCollection<Databases.Voditel>(Databases.GetVoditelList());
            _selectedVoditel = Voditels.First();
        }

        private readonly DelegateCommand _sendCommand;
        public DelegateCommand SendCommand
        {
            get
            {
                return _sendCommand;
            }
        }
        private void Send()
        {
            var newPosilkaId = Databases.CreateNewPosilka();
            foreach (var tovar in TovarsTemp)
            {
                Databases.AddtovarPosilka(new Databases.TovarPosilka{PosilkaID = newPosilkaId,TovarID = tovar.ID});
            }
            Databases.AddPosilkaMagazin(new Databases.PosilkaMagazin{MagazinID = SelectedMagaz.ID, PosilkaID = newPosilkaId});
            Databases.AddPosilkaVoditel(new Databases.PosilkaVoditel{PosilkaID = newPosilkaId, VoditelID = SelectedVoditel.ID});
        }

        private readonly DelegateCommand<Databases.Tovar> _delCommand;
        public DelegateCommand<Databases.Tovar> DelCommand
        {
            get
            {
                return _delCommand;
            }
        }
        private void DelTovar(Databases.Tovar tovar)
        {
            TovarsTemp.Remove(tovar);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private readonly DelegateCommand _addCommand;
        public DelegateCommand AddCommand
        {
            get
            {
                return _addCommand;
            }
        }
        private void AddNewTovar()
        {
            if (SelectedTovar == null) return; 
            TovarsTemp.Add(SelectedTovar);
            Tovars.Remove(SelectedTovar);
            if (Tovars.Count > 0)
            {
                SelectedTovar = Tovars.First();
            }
            else
            {
                SelectedTovar = null;
            }
        }
    }
}
